lib: Use libglnx file replace API more consistently
authorColin Walters <walters@verbum.org>
Thu, 4 Aug 2016 17:29:13 +0000 (13:29 -0400)
committerAtomic Bot <atomic-devel@projectatomic.io>
Thu, 4 Aug 2016 21:27:32 +0000 (21:27 +0000)
We have a better API now, drop use of the internal helper, which also
depended on libgsystem.

This required bumping libglnx to pull in a fix.

Closes: #429
Approved by: giuseppe

libglnx
src/libostree/ostree-repo-pull.c
src/libotutil/ot-gio-utils.c

diff --git a/libglnx b/libglnx
index c2ba4d879956436c1349acb0aeafd6f885276c67..5ac0d702d70b00887f9329e47f4d5653e994531a 160000 (submodule)
--- a/libglnx
+++ b/libglnx
@@ -1 +1 @@
-Subproject commit c2ba4d879956436c1349acb0aeafd6f885276c67
+Subproject commit 5ac0d702d70b00887f9329e47f4d5653e994531a
index 7d9f61b01bf0ed4fde29d92ddb7b1cd759caa56c..712f17dc1fd0bd82c7c8ae65d245aa9d9146dd1d 100644 (file)
@@ -2791,16 +2791,24 @@ ostree_repo_pull_with_options (OstreeRepo             *self,
 
   if (pull_data->is_mirror && pull_data->summary_data)
     {
-      if (!ot_file_replace_contents_at (pull_data->repo->repo_dir_fd, "summary",
-                                        pull_data->summary_data, !pull_data->repo->disable_fsync,
-                                        cancellable, error))
+      GLnxFileReplaceFlags replaceflag =
+        pull_data->repo->disable_fsync ? GLNX_FILE_REPLACE_NODATASYNC : 0;
+      gsize len;
+      const guint8 *buf = g_bytes_get_data (pull_data->summary_data, &len);
+
+      if (!glnx_file_replace_contents_at (pull_data->repo->repo_dir_fd, "summary",
+                                          buf, len, replaceflag,
+                                          cancellable, error))
         goto out;
 
-      if (pull_data->summary_data_sig &&
-          !ot_file_replace_contents_at (pull_data->repo->repo_dir_fd, "summary.sig",
-                                        pull_data->summary_data_sig, !pull_data->repo->disable_fsync,
-                                        cancellable, error))
-        goto out;
+      if (pull_data->summary_data_sig)
+        {
+          buf = g_bytes_get_data (pull_data->summary_data_sig, &len);
+          if (!glnx_file_replace_contents_at (pull_data->repo->repo_dir_fd, "summary.sig",
+                                              buf, len, replaceflag,
+                                              cancellable, error))
+            goto out;
+        }
     }
 
   if (!ostree_repo_commit_transaction (pull_data->repo, NULL, cancellable, error))
index 4ff2fedac9c1a26d07d50cfdae7359b5a799127b..a7fe74cf55b955a4105b34e8e9b49e34180ab4fe 100644 (file)
@@ -275,76 +275,6 @@ ot_gfile_load_contents_utf8_allow_noent (GFile          *path,
   return ret;
 }
 
-/**
- * ot_file_replace_contents_at:
- * 
- * Like g_file_replace_contents(), except using a fd-relative
- * directory, and optionally enforces use of fdatasync().
- */
-gboolean
-ot_file_replace_contents_at (int             dfd,
-                             const char     *path,
-                             GBytes         *contents,
-                             gboolean        datasync,
-                             GCancellable   *cancellable,
-                             GError        **error)
-{
-  gboolean ret = FALSE;
-  int fd;
-  g_autofree char *tmpname = NULL;
-  g_autoptr(GOutputStream) stream = NULL;
-  g_autoptr(GInputStream) instream = NULL;
-
-  if (!gs_file_open_in_tmpdir_at (dfd, 0644,
-                                  &tmpname, &stream,
-                                  cancellable, error))
-    goto out;
-
-  g_assert (G_IS_FILE_DESCRIPTOR_BASED (stream));
-  fd = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (stream));
-
-  instream = g_memory_input_stream_new_from_bytes (contents);
-
-  if (g_bytes_get_size (contents) > 0)
-    {
-      int r = posix_fallocate (fd, 0, g_bytes_get_size (contents));
-      if (r != 0)
-        {
-          /* posix_fallocate is a weird deviation from errno standards */
-          errno = r;
-          glnx_set_error_from_errno (error);
-          goto out;
-        }
-    }
-
-  if (g_output_stream_splice (stream, instream, 0,
-                              cancellable, error) < 0)
-    goto out;
-
-  if (datasync && fdatasync (fd) != 0)
-    {
-      glnx_set_error_from_errno (error);
-      goto out;
-    }
-
-  if (!g_output_stream_close (stream, cancellable, error))
-    goto out;
-
-  if (renameat (dfd, tmpname, dfd, path) == -1)
-    {
-      glnx_set_error_from_errno (error);
-      goto out;
-    }
-
-  g_clear_pointer (&tmpname, g_free);
-
-  ret = TRUE;
- out:
-  if (tmpname)
-    (void) unlinkat (dfd, tmpname, 0);
-  return ret;
-}
-
 /**
  * ot_gfile_replace_contents_fsync:
  * 
@@ -356,25 +286,13 @@ ot_gfile_replace_contents_fsync (GFile          *path,
                                  GCancellable   *cancellable,
                                  GError        **error)
 {
-  gboolean ret = FALSE;
-  glnx_fd_close int parent_dfd = -1;
-  const char *target_basename = glnx_basename (gs_file_get_path_cached (path));
-  g_autoptr(GFile) parent = NULL;
-
-  parent = g_file_get_parent (path);
-
-  if (!glnx_opendirat (AT_FDCWD, gs_file_get_path_cached (parent), TRUE,
-                       &parent_dfd, error))
-    goto out;
+  gsize len;
+  const guint8*buf = g_bytes_get_data (contents, &len);
 
-  if (!ot_file_replace_contents_at (parent_dfd, target_basename,
-                                    contents, TRUE,
-                                    cancellable, error))
-    goto out;
-
-  ret = TRUE;
- out:
-  return ret;
+  return glnx_file_replace_contents_at (AT_FDCWD, gs_file_get_path_cached (path),
+                                        buf, len,
+                                        GLNX_FILE_REPLACE_DATASYNC_NEW,
+                                        cancellable, error);
 }
 
 /**